//
//  PhoneViewController.m
//  Cxzc
//
//  Created by hwd on 17/8/7.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "ChangePhoneViewController.h"
#import "TextFiledView.h"
#import "ChangeCell.h"
@interface ChangePhoneViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableV;
@property (nonatomic,strong)UIButton *sendBtn;//验证码发送
@property (nonatomic,strong)NSString *phoneStr;//手机号
@property (nonatomic,strong)NSString *CodeStr;//验证码
@property (nonatomic,strong)NSString *xinPhoneStr;//新手机号
@property (nonatomic,strong)NSTimer *timer;//定时器
@property (nonatomic,assign)int s;//秒
@property (nonatomic,strong)NSArray *placeHodelArr;//输入框提示Arr
@property (nonatomic,strong)CanLab *saveBtn;//保存按钮
@property (nonatomic,strong)UIView *footView;//区尾
@end

@implementation ChangePhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = backColor;
    [self initTitleWithTitle:@"修改手机号"];
    [self initLeftBarButtonWithImage:@"back"];
    [self.view addSubview:self.tableV];
}
-(NSArray *)placeHodelArr//初始化数组
{
    if (!_placeHodelArr) {
        _placeHodelArr = @[@"旧手机号",@"短信验证码",@"新手机号"];
    }
    return _placeHodelArr;
}
//创建TableView
-(UITableView *)tableV
{
    if (!_tableV) {
        _tableV = [[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREENW, SCREENH-64-49) style:UITableViewStyleGrouped];
        _tableV.backgroundColor = backColor;
        _tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableV.showsVerticalScrollIndicator = NO;
        _tableV.rowHeight = 50*screenW_6;
        _tableV.delegate = self;
        _tableV.dataSource = self;
    }
    return _tableV;
}

#pragma Mark TableViewDelegate
//行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.placeHodelArr.count;
}
//区头高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.00001;
}
//区尾高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 80*screenW_6;
}
//加载Cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //加载输入框
    ChangeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChangeCell"];
    if (cell == nil) {
        cell = [[ChangeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ChangeCell"];
    }
    [cell loadCellInputStr:@"" PlaceHodelStr:@"" Title:self.placeHodelArr[indexPath.row]];
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (!_footView) {//保存
        _footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, 80*screenW_6)];
        _footView.backgroundColor = backColor;
        _saveBtn = [[CanLab alloc]initWithFrame:CGRectMake(10*screenW_6,30*screenW_6,SCREENW-20*screenW_6 ,44*screenW_6) textAligment:1 font:16*screenW_6 textColor:@"#ffffff" text:@"保存"];
        _saveBtn.backgroundColor = [UIColor colorWithHexString:@"#2cb4f1"];
        [_saveBtn.layer setCornerRadius:5*screenW_6];
        _saveBtn.layer.masksToBounds = YES;
        [_footView addSubview:_saveBtn];
        _saveBtn.block = ^(CanLab *lab)//保存点击事件
        {
            
        };
    }
    return _footView;
}

-(UIButton *)sendBtn//获取验证码Btn
{
    if (!_sendBtn) {//不存在
//        _sendBtn = [TomamaUI createButton:UIButtonTypeCustom Frame:CGRectMake(CGRectGetMaxX(_CodeTF.frame)+15*screenW_6, CGRectGetMaxY(_phoneTF.frame)+16*screenW_6,115*screenW_6, 45*screenW_6) targ:self sel:@selector(sendSelect) titleColor:TextRed font:[UIFont systemFontOfSize:17*screenW_6] title:@"获取验证码" image:@"" backGroundImage:@""];
//        [_sendBtn.layer setCornerRadius:5*screenW_6];
//        _sendBtn.backgroundColor = [UIColor whiteColor];
//        _sendBtn.layer.masksToBounds = YES;
//        _sendBtn.layer.borderWidth = 1;
//        _sendBtn.layer.borderColor = [TextRed CGColor];
    }
    return _sendBtn;
}

-(void)sendSelect//发送验证码
{
    _sendBtn.userInteractionEnabled = NO;
    if (_phoneStr.length==11) {//手机验证11位
        _s = 60;
        [_sendBtn setTitle:@"60秒后重发" forState:UIControlStateNormal];
        _sendBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
//        [HttpRequest PostrequestUrl:@"/fluentDrive/interface/user/updateSendverificationCode" Dic:@{@"oldPhone":_dataDic[@"phone"],@"phone":_phoneStr} completion:^(NSDictionary *responseDic) {
//            if (![responseDic[@"success"] boolValue]) {//请求失败 重置定时器 及按钮
//                [HudHelper showHudWithMessage:responseDic[@"msg"]?responseDic[@"msg"]:@"验证码发送失败" toView:self.view];
//                [_sendBtn setTitle:@"重发验证码" forState:UIControlStateNormal];
//                _sendBtn.userInteractionEnabled = YES;
//                [_timer invalidate];
//                _timer = nil;
//            }
//        }];
    }
    else//验证失败
    {
        [ToolViewVC addKeyAlterViewController:@"手机号码格式不正确" Controller:self];
        _sendBtn.userInteractionEnabled = YES;
    }
}
//定时器
-(void)onTimer
{
    _s--;
    NSString *timeStr = [NSString stringWithFormat:@"%d秒后重发",_s];
    [_sendBtn setTitle:timeStr forState:UIControlStateNormal];
    if (_s==0)//秒数为0 重置按钮定时器
    {
        [_sendBtn setTitle:@"重发验证码" forState:UIControlStateNormal];
        _sendBtn.userInteractionEnabled = YES;
        [_timer invalidate];
        _timer = nil;
    }
}
-(void)onTapRightBarButton//保存
{
    [self.view endEditing:YES];
    [HudHelper showProgressHudWithMessage:@"" toView:self.view];
    //验证手机号
    if (_phoneStr.length!=11) {
        [HudHelper hideHudToView:self.view];
        [TomamaUI creatAlertWithTarget:self message:@"请正确填写手机号码" timeInterval:2];
        return;
    }
    //手机验证码判空
    if (_CodeStr.length <= 0) {
        [HudHelper hideHudToView:self.view];
        [TomamaUI creatAlertWithTarget:self message:@"请正确填写手机验证码" timeInterval:2];
        return;
    }
    
//    [HudHelper showProgressHudWithMessage:@"" toView:self.view];
//    dispatch_async(dispatch_get_global_queue(0, 0), ^{
//        [HttpRequest PostrequestUrl:@"/fluentDrive/interface/user/updatePhone" Dic:@{@"id":[Storage getToken],@"phone":[ToolViewVC getNotNullStr:_phoneStr],@"verificationCode":_CodeStr} completion:^(NSDictionary *responseDic) {
//            dispatch_async(dispatch_get_main_queue(), ^{
//                [HudHelper hideHudToView:self.view];
//                if ([responseDic[@"success"] boolValue]) {//请求成功
//                    [ToolViewVC addPopAlterViewController:@"修改成功" Controller:self];
//                }else{//请求失败
//                    [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"]?responseDic[@"msg"]:@"修改失败" timeInterval:2];
//                }
//            });
//        }];
//    });
}
@end
